public class NnMatrix {
    public static void main(String[] args) {
        test.matrix(5);
    }
}
class test{
    public static void matrix(int n) {
        int arr[][] = new int[n][n];
        int count = 1;
        int left = 0;
        int right = n - 1;
        int top = 0;
        int bottom = n - 1;
        while (count <= n*n){
            for (int i = left;i<=right;i++){
                arr[top][i] = count++;
            }
            top++;
            for (int i = top;i<=bottom;i++){
                arr[i][right] = count++;
            right--;
            for (int i = right;i>= left;i--){
                arr[bottom][i] = count++;
            }
            bottom--;
            for (int i = bottom;i>= top;i-- ){
                arr[i][left] = count++;
            }
            left++;
        }
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }
}